<template>

	<div>
		<button type="primary" @click="initBleSDK">初始化</button>
		<button type="primary" @click="isSupportBle">是否支持BLE蓝牙</button>
		<button type="primary" @click="isBlueEnable">蓝牙是否开启</button>
		<button type="primary" @click="enableBluetooth">打开蓝牙</button>
		<button type="primary" @click="disableBluetooth">关闭蓝牙</button>
		<button type="primary" @click="startScan">开始扫描</button>
		<button type="primary" @click="stopScan">停止扫描</button>
		<button type="primary" @click="connect">连接</button>
		<button type="primary" @click="disconnect">断开连接</button>
		<button type="primary" @click="read">读取</button>
		<button type="primary" @click="readRssi">读取rssi</button>
		<button type="primary" @click="setMtu">写入mtu</button>
		<button type="primary" @click="write">写入</button>
		<button type="primary" @click="notify">开始notify</button>
		<button type="primary" @click="stopNotify">停止notify</button>
		<button type="primary" @click="release">释放资源</button>
	</div>
</template>

<script>
	// 获取 module 
	var sdkModule = uni.requireNativePlugin("XM-BleModule2")
	const modal = uni.requireNativePlugin('modal');
	export default {
		onLoad() {

		},
		methods: {
			//初始化
			initBleSDK() {
				//不需要设置的参数请注释掉
				sdkModule.initBleSDK({
					logEnable: false, // 设置是否输出打印蓝牙日志,uniapp无法看到，故设置为false
					connectRetryCount: 1, //连接异常时（如蓝牙协议栈错误）,重新连接次数
					connectTimeout: 30, //连接超时时间，单位秒
					operateTimeout: 30, //操作超时时间，单位秒
					splitWriteNum: 20, //写入分片大小，默认20个字节
					//1. 如果项目中的设备是统一类型(服务,特征uuid相同),则推荐在初始化时把服务,特征的uuid配置完整。
					//2. 如果项目中需要兼容多种设备类型(服务,特征uuid不相同),则在通信时传入对应的Uuid的方式进行。
					uuidService: '',//设置主服务的uuid（选填）
					uuidWrite: '',//设置可写特征的uuid（选填）
					uuidRead: '',//设置可读特征的uuid （选填）
					uuidNotify: ''//设置可通知特征的uuid （选填，库中默认已匹配可通知特征的uuid）
				}, (ret) => {
					//扫描回调结果
					console.log(ret)
					modal.toast({
						//发送操作结果
						message: ret,
						duration: 1.5
					});
				});
			},
			isSupportBle() {
				var ret = sdkModule.isSupportBle();
				console.log(ret)
			},
			isBlueEnable() {
				var ret = sdkModule.isBlueEnable();
				console.log(ret)
			},
			enableBluetooth() {
				sdkModule.enableBluetooth();
			},
			disableBluetooth() {
				sdkModule.disableBluetooth();
			},
			//开始扫描
			startScan() {
				sdkModule.startScan({
					uuids: [] //过滤的uuid数组
				}, (ret) => {
					//扫描回调结果
					//console.log(ret.data.bleName)
					console.log(ret)
					modal.toast({
						//发送操作结果
						message: ret,
						duration: 1.5
					});
				});
			},
			//停止扫描
			stopScan() {
				sdkModule.stopScan();
			},
			connect() {
				sdkModule.connect({
					macAddress: 'E2:E0:5A:6D:37:8F' //mac地址
				}, (ret) => {
					//扫描回调结果
					console.log(ret)
					modal.toast({
						//发送操作结果
						message: ret,
						duration: 1.5
					});
				});
			},
			disconnect() {
				sdkModule.disconnect({
					macAddress: 'E2:E0:5A:6D:37:8F' //mac地址
				}, (ret) => {
					//扫描回调结果
					console.log(ret)
					modal.toast({
						//发送操作结果
						message: ret,
						duration: 1.5
					});
				});
			},
			read() {
				sdkModule.read({
					macAddress: 'E2:E0:5A:6D:37:8F', //mac地址
					uuid_service: '',//可空
					uuid_characteristic_read: ''//可空
				}, (ret) => {
					//扫描回调结果
					console.log(ret)
					modal.toast({
						//发送操作结果
						message: ret,
						duration: 1.5
					});
				});
			},
			readRssi() {
				sdkModule.readRssi({
					macAddress: '' //mac地址
				}, (ret) => {
					//扫描回调结果
					console.log(ret)
					modal.toast({
						//发送操作结果
						message: ret,
						duration: 1.5
					});
				});
			},
			setMtu() {
				sdkModule.setMtu({
					macAddress: '', //mac地址
					mtu: 128//大多数Android手机都支持
				}, (ret) => {
					//扫描回调结果
					console.log(ret)
					modal.toast({
						//发送操作结果
						message: ret,
						duration: 1.5
					});
				});
			},
			write() {
				sdkModule.write({
					macAddress: '', //mac地址
					uuid_service: '',//可空
					uuid_characteristic_write: '',//可空
					bytes: [0xFF,0xFE,0xFD,0xFC,0xFB,0xFA],//同时输入时优先
					hexStr: 'FFFEFDFCFBFA'//bytes为空时可用
				}, (ret) => {
					//扫描回调结果
					console.log(ret)
					modal.toast({
						//发送操作结果
						message: ret,
						duration: 1.5
					});
				});
			},
			notify() {
				sdkModule.notify({
					macAddress: 'E2:E0:5A:6D:37:8F', //mac地址
					uuid_service: '0000fff0-0000-1000-8000-00805f9b34fb',//可空
					uuid_characteristic_notify: '0000fff1-0000-1000-8000-00805f9b34fb'//可空
				}, (ret) => {
					//扫描回调结果
					console.log(ret)
					modal.toast({
						//发送操作结果
						message: ret,
						duration: 1.5
					});
				});
			},
			stopNotify() {
				sdkModule.stopNotify({
					macAddress: '', //mac地址
					uuid_service: '',//可空
					uuid_characteristic_notify: ''//可空
				}, (ret) => {
					//扫描回调结果
					console.log(ret)
					modal.toast({
						//发送操作结果
						message: ret,
						duration: 1.5
					});
				});
			},
			release() {
				sdkModule.release();
			}
		}
	}
</script>